package com.ypay.demo.wechat;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ypay.apppay.ServiceFactory;
import com.yzhn.demo.R;

public class MainActivity extends AppCompatActivity {


    private EditText etAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etAmount = findViewById(R.id.et_amount);

        findViewById(R.id.tv_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkEmpty(R.id.tv_pay);
            }
        });
        findViewById(R.id.tv_alipay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkEmpty(R.id.tv_alipay);
            }
        });

    }

    private void checkEmpty(int i) {


        String amount = etAmount.getText().toString();
        if (TextUtils.isEmpty(amount)) {
            Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
        } else {
            int totalAmount = ((int) (Float.parseFloat(amount) * 100));
            switch (i) {
                case R.id.tv_pay:
                    ServiceFactory.payWechat(this,   "hn", totalAmount + "" );
                    break;
                case R.id.tv_alipay:
                    final Context context=this;
                    ServiceFactory.payAlipay(this,  "video", totalAmount + ""  , new Handler() {
                        @SuppressWarnings("unused")
                        public void handleMessage(Message msg) {
                            if(ServiceFactory.isSucess(msg)){
                                Toast.makeText(context, "订单支付成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                    break;
                default:
                    Toast.makeText(this, "无此支付方式", Toast.LENGTH_SHORT).show();

            }


        }

    }

}
